﻿
namespace S7_3UBD
{
    /// <summary>
    /// 用户表数据访问层类
    /// </summary>
    public class UserDBAccess : DBAccessBase
    {
        // private string connStr = "Server=.\\SQLEXPRESS;DataBase=tz1989_fw;Trusted_Connection=true;";
        /// <summary>
        /// 获取用户列表
        /// </summary>
        /// <returns></returns>
        public List<Base_User> GetList()
        {
            using (var context = GetDBContext())
            {
                var list = context.Base_User.ToList();
                return list;
            }
        }
        /// <summary>
        /// 通过委托传出context,执行外部方法
        /// </summary>
        /// <param name="entity"></param>
        public void Add(Base_User entity, DBHandler handler = null)
        {
            using (var context = GetDBContext())
            {
                var tran = context.BeginTransaction();
                try
                {
                    entity.Id = Guid.NewGuid().ToString();
                    entity.CreateTime = DateTime.Now;
                    context.Base_User.Add(entity);
                    if (handler != null)
                    {
                        handler(context);
                    }
                    int row = context.SaveChanges();
                    tran.Commit();
                }
                catch (Exception)
                {
                    tran.Rollback();
                    throw;
                }
            }
        }
    }

    public delegate void DBHandler(TZ1989DBContext context);

}
